using System.Collections;
using System.Reflection;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class ChatboxSimpleText : MonoBehaviour
{
	public Transform TextboxObject;

	public TextMeshProUGUI TextUI;

	public AudioSource TextVoiceEmitter;

	public int CurrentTextIndex;

	public int CurrentAdditionalTextIndex;

	public int PauseCounter;

	public CHATBOXTEXT storedchatboxtext;

	public bool ChatIsCurrentlyRunning;

	public TMP_FontAsset DefaultFont;

	public AudioClip DefaultSpeakSound;

	public Sprite DefaultIcon;

	private float CurrentTextSpeedMultiplier;

	public void RunText(CHATBOXTEXT Chatbox, int index)
	{
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		((MonoBehaviour)this).StopCoroutine("PlayText");
		ChatIsCurrentlyRunning = true;
		CurrentAdditionalTextIndex = index;
		CurrentTextSpeedMultiplier = 1f;
		((Graphic)TextUI).color = Color.white;
		if ((Object)(object)Chatbox.Textboxes[index].Character[CurrentTextIndex] == (Object)null || (Object)(object)Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterSound == (Object)null)
		{
			TextVoiceEmitter.clip = DefaultSpeakSound;
		}
		else
		{
			TextVoiceEmitter.clip = Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterSound;
		}
		((Behaviour)TextUI).enabled = true;
		((TMP_Text)TextUI).text = "";
		storedchatboxtext = Chatbox;
		if ((Object)(object)Chatbox.Textboxes[index].Character[CurrentTextIndex] != (Object)null && Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterFontSize != 0f)
		{
			((TMP_Text)TextUI).fontSize = Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterFontSize;
		}
		else
		{
			((TMP_Text)TextUI).fontSize = 64f;
		}
		((MonoBehaviour)this).StartCoroutine("PlayText");
	}

	public void EndText()
	{
		((Behaviour)TextUI).enabled = false;
		storedchatboxtext = null;
		PauseCounter = 0;
		CurrentTextIndex = 0;
		CurrentAdditionalTextIndex = 0;
		((MonoBehaviour)this).StopCoroutine("PlayText");
		ChatIsCurrentlyRunning = false;
	}

	private IEnumerator PlayText()
	{
		string text = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Text[CurrentTextIndex];
		text = text.Replace(';', '\n');
		((TMP_Text)TextUI).text = text;
		((TMP_Text)TextUI).maxVisibleCharacters = 0;
		AudioClip talkClip = ((!((Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex] != (Object)null) || !((Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex].CharacterSound != (Object)null)) ? DefaultSpeakSound : storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex].CharacterSound);
		if ((Object)(object)storedchatboxtext != (Object)null && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex] != null && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action.Length != 0 && (Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex] != (Object)null && !storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex].RunActionOnChatEnd)
		{
			CHATBOXACTION cHATBOXACTION = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex];
			if (cHATBOXACTION.PlaySound && cHATBOXACTION.PossibleSounds.Length != 0)
			{
				AudioClip val = cHATBOXACTION.PossibleSounds[Random.Range(0, cHATBOXACTION.PossibleSounds.Length)];
				TextVoiceEmitter.PlayOneShot(val);
			}
			GameObject val2 = GameObject.Find(cHATBOXACTION.TargetComponentGameObjectName);
			if (cHATBOXACTION.RunComponentFunction && (Object)(object)val2 != (Object)null && cHATBOXACTION.FunctionName != null)
			{
				string targetComponentClassname = cHATBOXACTION.TargetComponentClassname;
				Component component = val2.GetComponent(targetComponentClassname);
				if ((Object)(object)component != (Object)null)
				{
					if (((object)component).GetType().GetMethod(cHATBOXACTION.FunctionName) != null)
					{
						((object)component).GetType().GetMethod(cHATBOXACTION.FunctionName).Invoke(component, null);
					}
					else
					{
						MonoBehaviour.print((object)"did you forget to make the method public?");
					}
				}
				else
				{
					MonoBehaviour.print((object)("Couldn't find Component named: " + targetComponentClassname));
				}
			}
		}
		AttemptRunSubActions();
		AttemptRunMultipleActions();
		int messageCharLength = text.Length;
		char[] messageCharacters = text.ToCharArray();
		while (((TMP_Text)TextUI).maxVisibleCharacters < messageCharLength)
		{
			char c = messageCharacters[((TMP_Text)TextUI).maxVisibleCharacters];
			if (c == ' ' || c == '(' || c == ')')
			{
				TextMeshProUGUI textUI = TextUI;
				int maxVisibleCharacters = ((TMP_Text)textUI).maxVisibleCharacters;
				((TMP_Text)textUI).maxVisibleCharacters = maxVisibleCharacters + 1;
				yield return (object)new WaitForSeconds(0.08f * CurrentTextSpeedMultiplier);
			}
			else
			{
				yield return (object)new WaitForSeconds(0.08f * CurrentTextSpeedMultiplier);
				TextVoiceEmitter.PlayOneShot(talkClip);
				TextMeshProUGUI textUI2 = TextUI;
				int maxVisibleCharacters = ((TMP_Text)textUI2).maxVisibleCharacters;
				((TMP_Text)textUI2).maxVisibleCharacters = maxVisibleCharacters + 1;
			}
		}
		if ((Object)(object)storedchatboxtext != (Object)null && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex] != null && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action.Length != 0 && (Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex] != (Object)null && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex].RunActionOnChatEnd)
		{
			CHATBOXACTION cHATBOXACTION2 = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex];
			if (cHATBOXACTION2.PlaySound && cHATBOXACTION2.PossibleSounds.Length != 0)
			{
				AudioClip val3 = cHATBOXACTION2.PossibleSounds[Random.Range(0, cHATBOXACTION2.PossibleSounds.Length)];
				TextVoiceEmitter.PlayOneShot(val3);
			}
			GameObject val4 = GameObject.Find(cHATBOXACTION2.TargetComponentGameObjectName);
			if (cHATBOXACTION2.RunComponentFunction && (Object)(object)val4 != (Object)null && cHATBOXACTION2.FunctionName != null)
			{
				string targetComponentClassname2 = cHATBOXACTION2.TargetComponentClassname;
				Component component2 = val4.GetComponent(targetComponentClassname2);
				if ((Object)(object)component2 != (Object)null)
				{
					if (((object)component2).GetType().GetMethod(cHATBOXACTION2.FunctionName) != null)
					{
						((object)component2).GetType().GetMethod(cHATBOXACTION2.FunctionName).Invoke(component2, null);
					}
					else
					{
						MonoBehaviour.print((object)"did you forget to make the method public?");
					}
				}
				else
				{
					MonoBehaviour.print((object)("Couldn't find Component named: " + targetComponentClassname2));
				}
			}
		}
		AttemptRunSubActions(IsChatEnd: true);
		AttemptRunMultipleActions(IsChatEnd: true);
	}

	private void AttemptRunActions()
	{
		Debug.Log((object)"AttemptRunActions started.");
		if (storedchatboxtext.Textboxes == null)
		{
			Debug.LogError((object)"storedchatboxtext.Textboxes is null");
			return;
		}
		if (storedchatboxtext.Textboxes[CurrentAdditionalTextIndex] == null)
		{
			Debug.LogError((object)"storedchatboxtext.Textboxes[CurrentAdditionalTextIndex] is null");
			return;
		}
		Textbox textbox = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex];
		if (textbox.Action == null)
		{
			Debug.LogError((object)"currentTextbox.Action is null");
			return;
		}
		if (textbox.Action.Length == 0)
		{
			Debug.Log((object)"currentTextbox.Action.Length is 0");
			return;
		}
		if ((Object)(object)textbox.Action[CurrentTextIndex] == (Object)null)
		{
			Debug.Log((object)"currentTextbox.Action[CurrentTextIndex] is null");
			return;
		}
		CHATBOXACTION cHATBOXACTION = textbox.Action[CurrentTextIndex];
		if (!cHATBOXACTION.RunActionOnChatEnd)
		{
			Debug.Log((object)"action.RunActionOnChatEnd is false");
			return;
		}
		Debug.Log((object)("Action will be run: " + ((object)cHATBOXACTION).ToString()));
		if (cHATBOXACTION.PlaySound)
		{
			if (cHATBOXACTION.PossibleSounds == null)
			{
				Debug.LogError((object)"action.PossibleSounds is null");
				return;
			}
			if (cHATBOXACTION.PossibleSounds.Length == 0)
			{
				Debug.LogError((object)"action.PossibleSounds.Length is 0");
				return;
			}
			AudioClip val = cHATBOXACTION.PossibleSounds[Random.Range(0, cHATBOXACTION.PossibleSounds.Length)];
			Debug.Log((object)("Playing sound: " + ((Object)val).name));
			TextVoiceEmitter.PlayOneShot(val);
		}
		if (!cHATBOXACTION.RunComponentFunction)
		{
			return;
		}
		if (cHATBOXACTION.TargetComponentGameObjectName == null)
		{
			Debug.LogError((object)"action.TargetComponentGameObjectName is null");
			return;
		}
		GameObject val2 = GameObject.Find(cHATBOXACTION.TargetComponentGameObjectName);
		if ((Object)(object)val2 == (Object)null)
		{
			Debug.LogError((object)("TargetGameObject not found: " + cHATBOXACTION.TargetComponentGameObjectName));
			return;
		}
		if (cHATBOXACTION.FunctionName == null)
		{
			Debug.LogError((object)"action.FunctionName is null");
			return;
		}
		string targetComponentClassname = cHATBOXACTION.TargetComponentClassname;
		if (targetComponentClassname == null)
		{
			Debug.LogError((object)"action.TargetComponentClassname is null");
			return;
		}
		Component component = val2.GetComponent(targetComponentClassname);
		if ((Object)(object)component == (Object)null)
		{
			Debug.LogError((object)("Couldn't find Component named: " + targetComponentClassname));
			return;
		}
		MethodInfo method = ((object)component).GetType().GetMethod(cHATBOXACTION.FunctionName);
		if (method == null)
		{
			Debug.LogError((object)("Method not found: " + cHATBOXACTION.FunctionName));
			return;
		}
		Debug.Log((object)("Invoking method: " + cHATBOXACTION.FunctionName));
		method.Invoke(component, null);
		Debug.Log((object)"AttemptRunActions ended.");
	}

	private void AttemptRunMultipleActions(bool IsChatEnd = false)
	{
		Debug.Log((object)"AttemptRunMultipleActions started.");
		if ((Object)(object)storedchatboxtext == (Object)null)
		{
			Debug.Log((object)"storedchatboxtext is null");
			return;
		}
		if (storedchatboxtext.Textboxes == null)
		{
			Debug.Log((object)"storedchatboxtext.Textboxes is null");
			return;
		}
		if (storedchatboxtext.Textboxes[CurrentAdditionalTextIndex] == null)
		{
			Debug.LogError((object)"storedchatboxtext.Textboxes[CurrentAdditionalTextIndex] is null");
			return;
		}
		Textbox textbox = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex];
		if (textbox.SubActions == null)
		{
			Debug.Log((object)"currentTextbox.SubActions is null");
			return;
		}
		if (textbox.SubActions.Length == 0)
		{
			Debug.Log((object)"currentTextbox.SubActions.Length is 0");
			return;
		}
		if (textbox.SubActions[CurrentTextIndex].SubActions.Length == 0)
		{
			Debug.Log((object)"currentTextbox.SubActions.SubActions.Length is 0");
			return;
		}
		CHATBOXSUBACTION cHATBOXSUBACTION = textbox.SubActions[CurrentTextIndex];
		CHATBOXACTION[] subActions = cHATBOXSUBACTION.SubActions;
		foreach (CHATBOXACTION cHATBOXACTION in subActions)
		{
			if ((Object)(object)cHATBOXACTION == (Object)null)
			{
				Debug.Log((object)"Action is null, skipping");
			}
			else
			{
				if (cHATBOXACTION.RunActionOnChatEnd != IsChatEnd)
				{
					continue;
				}
				Debug.Log((object)("Action will be run: " + cHATBOXSUBACTION.ToString()));
				if (cHATBOXACTION.PlaySound)
				{
					if (cHATBOXACTION.PossibleSounds == null)
					{
						Debug.LogError((object)"action.PossibleSounds is null");
						continue;
					}
					if (cHATBOXACTION.PossibleSounds.Length == 0)
					{
						Debug.LogError((object)"action.PossibleSounds.Length is 0");
						continue;
					}
					AudioClip val = cHATBOXACTION.PossibleSounds[Random.Range(0, cHATBOXACTION.PossibleSounds.Length)];
					Debug.Log((object)("Playing sound: " + ((Object)val).name));
					TextVoiceEmitter.PlayOneShot(val);
				}
				if (!cHATBOXACTION.RunComponentFunction)
				{
					continue;
				}
				if (cHATBOXACTION.TargetComponentGameObjectName == null)
				{
					Debug.LogError((object)"action.TargetComponentGameObjectName is null");
					continue;
				}
				GameObject val2 = GameObject.Find(cHATBOXACTION.TargetComponentGameObjectName);
				if ((Object)(object)val2 == (Object)null)
				{
					Debug.LogError((object)("TargetGameObject not found: " + cHATBOXACTION.TargetComponentGameObjectName));
					continue;
				}
				if (cHATBOXACTION.FunctionName == null)
				{
					Debug.LogError((object)"action.FunctionName is null");
					continue;
				}
				string targetComponentClassname = cHATBOXACTION.TargetComponentClassname;
				if (targetComponentClassname == null)
				{
					Debug.LogError((object)"action.TargetComponentClassname is null");
					continue;
				}
				Component component = val2.GetComponent(targetComponentClassname);
				if ((Object)(object)component == (Object)null)
				{
					Debug.LogError((object)("Couldn't find Component named: " + targetComponentClassname));
					continue;
				}
				MethodInfo method = ((object)component).GetType().GetMethod(cHATBOXACTION.FunctionName);
				if (method == null)
				{
					Debug.LogError((object)("Method not found: " + cHATBOXACTION.FunctionName));
					continue;
				}
				Debug.Log((object)("Invoking method: " + cHATBOXACTION.FunctionName));
				method.Invoke(component, null);
			}
		}
		Debug.Log((object)"AttemptRunMultipleActions ended.");
	}

	private void AttemptRunSubActions(bool IsChatEnd = false)
	{
		if ((Object)(object)storedchatboxtext == (Object)null)
		{
			return;
		}
		if (storedchatboxtext.Textboxes[CurrentAdditionalTextIndex] == null)
		{
			Debug.LogError((object)"storedchatboxtext.Textboxes[CurrentAdditionalTextIndex] is null");
			return;
		}
		Textbox textbox = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex];
		if (textbox.Action == null)
		{
			Debug.LogError((object)"currentTextbox.Action is null");
			return;
		}
		if (textbox.Action.Length == 0)
		{
			Debug.Log((object)"currentTextbox.Action.Length is 0");
			return;
		}
		if ((Object)(object)textbox.Action[CurrentTextIndex] == (Object)null)
		{
			Debug.Log((object)"currentTextbox.Action[CurrentTextIndex] is null");
			return;
		}
		CHATBOXACTION obj = textbox.Action[CurrentTextIndex];
		CHATBOXACTION_SubAction[] subActions = obj.SubActions;
		if (obj.RunComponentFunction && subActions != null && subActions.Length != 0)
		{
			CHATBOXACTION_SubAction[] array = subActions;
			foreach (CHATBOXACTION_SubAction cHATBOXACTION_SubAction in array)
			{
				if (cHATBOXACTION_SubAction.RunActionOnChatEnd != IsChatEnd)
				{
					return;
				}
				if (cHATBOXACTION_SubAction.TargetComponentGameObjectName == null)
				{
					Debug.LogError((object)"action.TargetComponentGameObjectName is null");
					return;
				}
				GameObject val = GameObject.Find(cHATBOXACTION_SubAction.TargetComponentGameObjectName);
				if ((Object)(object)val == (Object)null)
				{
					Debug.LogError((object)("TargetGameObject not found: " + cHATBOXACTION_SubAction.TargetComponentGameObjectName));
					return;
				}
				if (cHATBOXACTION_SubAction.FunctionName == null)
				{
					Debug.LogError((object)"action.FunctionName is null");
					return;
				}
				string targetComponentClassname = cHATBOXACTION_SubAction.TargetComponentClassname;
				if (targetComponentClassname == null)
				{
					Debug.LogError((object)"action.TargetComponentClassname is null");
					return;
				}
				Component component = val.GetComponent(targetComponentClassname);
				if ((Object)(object)component == (Object)null)
				{
					Debug.LogError((object)("Couldn't find Component named: " + targetComponentClassname));
					return;
				}
				MethodInfo method = ((object)component).GetType().GetMethod(cHATBOXACTION_SubAction.FunctionName);
				if (method == null)
				{
					Debug.LogError((object)("Method not found: " + cHATBOXACTION_SubAction.FunctionName));
					return;
				}
				Debug.Log((object)("Invoking method: " + cHATBOXACTION_SubAction.FunctionName));
				method.Invoke(component, null);
			}
		}
		Debug.Log((object)"AttemptRunActions ended.");
	}
}
